package com.lc.cehui.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CollectAddApi;
import com.lc.cehui.http.request.CollectDelApi;
import com.lc.cehui.http.request.CompanyChildtypeShowApi;
import com.lc.cehui.http.response.BaseBean;
import com.lc.cehui.http.response.CompanyChildtypeShowBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.land.LoginActivity;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.utils.RouteUtils;
import io.rong.imlib.model.Conversation;

public class ResumeDetailsActivity extends AppActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.img_sex)
    ImageView imgSex;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_year)
    TextView tvYear;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.tv_post)
    TextView tvPost;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.tv_salary)
    TextView tvSalary;

    @BindView(R.id.rv_job_ex)
    RecyclerView rvJobEx;
    @BindView(R.id.rv_project_ex)
    RecyclerView rvProjectEx;
    @BindView(R.id.rv_teach_ex)
    RecyclerView rvTeachEx;
    @BindView(R.id.img)
    ShapeableImageView img;
    @BindView(R.id.check_text)
    CheckBox checkbox;
private List<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean> datalist1=new ArrayList<>();
    private List<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean> datalist2=new ArrayList<>();

    private List<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean> datalist3=new ArrayList<>();
    private BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder> jobadapter;
    private BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder> projectadapter;
    private BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder> teachadapter;
    private String type_pid;
    private String type_id;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_resumedetails;
    }

    @Override
    protected void initView() {

        jobadapter = new BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder>(R.layout.item_experience,datalist1) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean bean) {

                holder.setText(R.id.tv_name,bean.getTitle());
                holder.setText(R.id.tv_tag,bean.getName());
                holder.setText(R.id.tv_year, CourseUtils.getMillsToTime(bean.getStart_time())+"至"+CourseUtils.getMillsToTime(bean.getEnd_time()));
                holder.setText(R.id.tv_content,bean.getContent());
                holder.setText(R.id.tv_salary,bean.getSalary());


            }
        };
        rvJobEx.setAdapter(jobadapter);
        projectadapter = new BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder>(R.layout.item_project_experience,datalist2) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean bean) {
                holder.setText(R.id.tv_name,bean.getTitle());
                holder.setText(R.id.tv_year, CourseUtils.getMillsToTime(bean.getStart_time())+"至"+CourseUtils.getMillsToTime(bean.getEnd_time()));
                holder.setText(R.id.tv_content,bean.getContent());

            }
        };
        rvProjectEx.setAdapter(projectadapter);
        teachadapter = new BaseQuickAdapter<CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean, BaseViewHolder>(R.layout.item_experience,datalist3) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, CompanyChildtypeShowBean.DataBean.EducationBean.List1Bean bean) {
                holder.setText(R.id.tv_name,bean.getTitle());
                holder.setText(R.id.tv_tag,bean.getName());
                holder.setText(R.id.tv_year, CourseUtils.getMillsToTime(bean.getStart_time())+"至"+CourseUtils.getMillsToTime(bean.getEnd_time()));
                holder.setText(R.id.tv_content,bean.getContent());
                holder.setText(R.id.tv_salary,bean.getSalary());
            }
        };
        rvTeachEx.setAdapter(teachadapter);
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    collect_add();
                } else {
                    collect_del();
                }
            }
        });
    }

    @Override
    protected void initData() {
        company_childtype_show();
    }

    private void company_childtype_show() {
        EasyHttp.post(this)
                .api(new CompanyChildtypeShowApi().setUserid(SPUtil.get(this, IntentKey.USERID,0)).setJob_id(getIntent().getStringExtra("id")))
                .request(new HttpCallback<CompanyChildtypeShowBean>(this) {

                    @Override
                    public void onSucceed(CompanyChildtypeShowBean data) {
                        GlideApp.with(ResumeDetailsActivity.this).load(data.getData().getUser().getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into(img);
                        tvName.setText(data.getData().getUser().getCnname());
                        tvAge.setText(data.getData().getUser().getAge()+"");
                        tvState.setText(data.getData().getUser().getNature());
                        if (data.getData().getUser().getGender()==0){
                            imgSex.setImageResource(R.mipmap.nan);
                        }else {
                            imgSex.setImageResource(R.mipmap.nv);
                        }
                        tvId.setText("ID:"+data.getData().getUser().getType_id()+"");
                        tvAddress.setText(data.getData().getUser().getCity());
                        tvYear.setText(data.getData().getUser().getYears());
                        tvEducation.setText(data.getData().getUser().getEducation());

                        tvPost.setText(data.getData().getUser().getTitle());
                        tvSalary.setText(data.getData().getUser().getSalary());
                        tvTag.setText(data.getData().getUser().getWork_type());
                        datalist1.addAll(data.getData().getEducation().getList1());
                        datalist2.addAll(data.getData().getEducation().getList2());
                        datalist3.addAll(data.getData().getEducation().getList3());
                        id = data.getData().getUser().getId()+"";
                        type_pid = data.getData().getUser().getType_pid()+"";
                        type_id = data.getData().getUser().getType_id()+"";
                        if (data.getData().getUser().getIs_collect() == 1) {
                            checkbox.setChecked(false);
                            checkbox.setText("收藏");
                        } else {
                            checkbox.setChecked(true);
                            checkbox.setText("已收藏");
                        }
                        jobadapter.setNewData(datalist1);
                        jobadapter.notifyDataSetChanged();
                        projectadapter.setNewData(datalist2);
                        projectadapter.notifyDataSetChanged();
                        teachadapter.setNewData(datalist3);
                        teachadapter.notifyDataSetChanged();
                    }
                });
    }
    private void collect_del() {
        EasyHttp.post(this)
                .api(new CollectDelApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(2).setArticle_id(id).setStore_id("").setType_pid(type_pid).setType_id(type_id))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {

                        if (callBackCode(data)) {
                            checkbox.setText("收藏");
                            toast("已取消收藏");
                        }
                    }
                });
    }

    private void collect_add() {
        EasyHttp.post(this)
                .api(new CollectAddApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setType(2).setArticle_id(id).setStore_id("").setType_pid(type_pid).setType_id(type_id))
                .request(new HttpCallback<BaseBean>(this) {
                    @Override
                    public void onSucceed(BaseBean data) {
                        if (data.code==200) {
                            checkbox.setText("已收藏");
                        }
                    }
                });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_job_more, R.id.tv_project_more, R.id.tv_teach_more, R.id.tv_clone, R.id.tv_say})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_job_more:
                startActivity(new Intent(this, ChildtypeShowmoreActivity.class).putExtra("id",getIntent().getStringExtra("id")).putExtra("title","工作经验").putExtra("type","0"));

                break;
            case R.id.tv_project_more:
                startActivity(new Intent(this, ChildtypeShowmoreActivity.class).putExtra("id",getIntent().getStringExtra("id")).putExtra("title","项目经验").putExtra("type","1"));
                break;
            case R.id.tv_teach_more:
                startActivity(new Intent(this, ChildtypeShowmoreActivity.class).putExtra("id",getIntent().getStringExtra("id")).putExtra("title","教育经历").putExtra("type","2"));
                break;
            case R.id.tv_clone:
                break;
            case R.id.tv_say:
                if (SPUtil.get(ResumeDetailsActivity.this, IntentKey.RID,0)==1){
                    RouteUtils.routeToConversationActivity(ResumeDetailsActivity.this, Conversation.ConversationType.NONE, "2");
                } else {
                    RouteUtils.routeToConversationActivity(ResumeDetailsActivity.this, Conversation.ConversationType.NONE, "1");
                }




                break;
        }
    }
}
